﻿using System.Xml;

namespace _47_C_生成Xml文件的三种方式_第一种使用XmlDocument_
{
    internal class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();

            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0","utf-8",null);

            doc.AppendChild(xmlDeclaration);

            XmlNode studentInfo = doc.CreateElement("StudentInfo");
            doc.AppendChild(studentInfo);

            XmlNode student = doc.CreateElement("Student");
            studentInfo.AppendChild(student);
            XmlAttribute grade = doc.CreateAttribute("Grade");
            grade.Value = "一年级";
            student.Attributes.Append(grade);

            XmlAttribute teacher = doc.CreateAttribute("Teacher");
            grade.Value = "老师";
            student.Attributes.Append(grade);

            //添加子节点
            XmlNode name=doc.CreateElement("name");
            student.AppendChild(name);
            name.InnerText = "学生1";

            XmlNode age = doc.CreateElement("age");
            student.AppendChild(age);
            age.InnerText = "18";

            doc.Save("D:\\tests.xml");
        }
    }
}
